﻿using System;
using System.Drawing;
using System.Windows.Forms;
using ImageFan.ImageHandling;
using ImageFan.Forms;

namespace ImageFan.Controls
{
    internal partial class ThumbnailBox
        : UserControl
    {
        public ThumbnailBox(ThumbnailsSequence thumbnailsSequence, int thumbnailIndex,
                            ImageFile imageFile)
        {
            InitializeComponent();

            _thumbnailsSequence = thumbnailsSequence;
            _thumbnailIndex = thumbnailIndex;
            _imageFile = imageFile;

            SetLoadingImageThumbnail();
        }

        public int ThumbnailIndex
        {
            get { return _thumbnailIndex; }
            set { _thumbnailIndex = value; }
        }

        public ImageFile ImageFile
        {
            get { return _imageFile; }
        }

        public void SetThumbnailImageThread()
        {
            var anImage = _imageFile.Thumbnail;
            _imageFile.DisposeImageFromDisc();

            if (InvokeRequired)
                Invoke(new SetThumbnailImageThreadDelegate(SetThumbnailImageHelper), anImage);
            else
                SetThumbnailImageHelper(anImage);
        }

        public void ReadImageFromDiscThread()
        {
            _imageFile.ReadImageFromDisc();
        }

        public void SetBorder(BorderStyle borderStyle)
        {
            BorderStyle = borderStyle;
        }


        #region Protected

        protected override void OnLoad(EventArgs args)
        {
            base.OnLoad(args);

            panelThumbnailBox.Cursor = Cursor;
            panelThumbnailBox.MouseClick += new MouseEventHandler(Controls_MouseClick);

            foreach (Control aControl in panelThumbnailBox.Controls)
            {
                aControl.Cursor = Cursor;
                aControl.MouseClick += new MouseEventHandler(Controls_MouseClick);
            }
        }

        protected override void OnKeyDown(KeyEventArgs args)
        {
            base.OnKeyDown(args);

            if (args.KeyCode == Keys.Enter)
            {
                if (_thumbnailsSequence.CurrentThumbnail != null)
                {
                    ShowImageForm();
                }
            }
            else if ((args.KeyCode == Keys.Down) || (args.KeyCode == Keys.S) ||
                     (args.KeyCode == Keys.Right) || (args.KeyCode == Keys.D))
                _thumbnailsSequence.MoveToNextThumbnail();
            else if ((args.KeyCode == Keys.Up) || (args.KeyCode == Keys.W) ||
                     (args.KeyCode == Keys.Left) || (args.KeyCode == Keys.A))
                _thumbnailsSequence.MoveToPreviousThumbnail();
        }

        protected override void OnMouseClick(MouseEventArgs args)
        {
            base.OnMouseClick(args);

            if (args.Button == MouseButtons.Left)
                ShowImageForm();
        }

        protected override bool IsInputKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                    return true;
                default:
                    return base.IsInputKey(keyData);
            }
        }

        #endregion


        #region Private

        private delegate void SetThumbnailImageThreadDelegate(Image thumbnail);

        private ThumbnailsSequence _thumbnailsSequence;
        private int _thumbnailIndex;

        private ImageFile _imageFile;

        private void Controls_MouseClick(object sender, MouseEventArgs args)
        {
            if (args.Button == MouseButtons.Left)
            {
                _thumbnailsSequence.MoveToThumbnail(this);
                ShowImageForm();
            }
        }

        private void SetThumbnailImageHelper(Image anImage)
        {
            pbThumbnail.Image = anImage;
        }

        private void SetLoadingImageThumbnail()
        {
            pbThumbnail.Image = GlobalData.LoadingImageThumbnail;
            lbImageName.Text = _imageFile.FileName;
        }

        private void ShowImageForm()
        {
            var imageForm = new ImageForm(_thumbnailsSequence,
                                          _thumbnailsSequence.CurrentThumbnail.ImageFile);
            imageForm.ShowDialog();
        }

        #endregion
    }
}
